package com.yixin.ystartlibrary.base;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.yixin.ystartlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Luozi
 * Date: 2015-05-22
 * Content:
 */
public abstract class BaseRecyclerAdapter<I, T extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<T> {

    private List<I> mInfoList;

    public BaseRecyclerAdapter() {
        mInfoList = new ArrayList<>();
    }

    public void addInfo(I i) {
        mInfoList.add(i);
    }

    public void addInfo(int location, I i) {
        mInfoList.add(location, i);
    }

    public void addInfoList(List<I> infoList) {
        mInfoList.addAll(infoList);
    }

    public void addInfoList(int location, List<I> infoList) {
        mInfoList.addAll(location, infoList);
    }

    public void setInfo(int location, I i) {
        mInfoList.set(location, i);
    }

    public void setAllList(List<I> infoList) {
        mInfoList = infoList;
    }

    public void removeInfo(int location) {
        mInfoList.remove(location);
    }

    public void removeInfo(I i) {
        mInfoList.remove(i);
    }

    public void removeInfoList(List<I> infoList) {
        mInfoList.removeAll(infoList);
    }

    public I getItem(int position) {
        return mInfoList.get(position);
    }

    public List<I> getAllInfoList() {
        return mInfoList;
    }

    public void clearInfoList() {
        mInfoList.clear();
    }

    public void replaceInfoList(List<I> infoList) {
        clearInfoList();
        mInfoList.addAll(infoList);
    }

    @Override
    public T onCreateViewHolder(ViewGroup parent, int viewType) {
        return getViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(T holder, int position) {
        bindListener(holder.itemView, position);
        onBindViewHolder(position, holder);
    }

    @Override
    public int getItemCount() {
        return mInfoList.size();
    }

    private View.OnClickListener mItemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag(R.id.base_adapter_key_position);
            onBindItemClickListener(v, position);
        }
    };
        private View.OnLongClickListener mItemLongClick= new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = (int) v.getTag(R.id.base_adapter_key_position);
                onBindItenLongClickListener(v,position);

                return false;
            }
        };


    /**
     * 绑定点击事件.
     *
     * @param v        绑定的view控件
     * @param position 绑定控件的位置
     */
    protected void bindListener(View v, int position) {
        v.setOnClickListener(mItemClick);
        v.setOnLongClickListener(mItemLongClick);
        v.setTag(R.id.base_adapter_key_position, position);
    }



    protected void onBindItemClickListener(View v, int position) {
        //item click
        }
        protected  void  onBindItenLongClickListener(View v,int position){

        }

    protected abstract void onBindViewHolder(int position, T holder);

    protected abstract T getViewHolder(ViewGroup parent, int viewType);

    public View inflate(int resId, ViewGroup parent) {
        return LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
    }
}
